package com.jerry.project;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.appengine.labs.repackaged.org.json.JSONArray;
import com.google.appengine.labs.repackaged.org.json.JSONException;




public class CreateEventServlet extends HttpServlet {
	@Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
        // We have one entity group per Guestbook with all Greetings residing
        // in the same entity group as the Guestbook to which they belong.
        // This lets us run a transactional ancestor query to retrieve all
        // Greetings for a given Guestbook.  However, the write rate to each
        // Guestbook should be limited to ~1/second.
		
        Key eventKey = KeyFactory.createKey("EventKind", "event");
        /*StringBuffer buf = new StringBuffer();
		char[] c = new char[1024];
		int len;
		while((len = req.getReader().read(c)) != -1) {
			buf.append(c, 0, len);
		}
		String result=buf.toString();
		try {
			JSONObject jsnobject = new JSONObject(result);
			//Object jsonOb = jsnobject.getJSONObject("id");
			String s=jsnobject.getString("id");
			s=jsnobject.toString();
		} catch (org.json.JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}*/
        JSONObject jsonobj=null;
        String id=null;
        String url=null;
        String name=null;
        Date startTime=null;
        String tags=null;
        try {
			jsonobj=new JSONObject(reqToJSONString.reqToJSONString(req));
			if(jsonobj.has("id")){
				id=jsonobj.getString("id");
			}
			if(jsonobj.has("url")){
				url=jsonobj.getString("url");
			}
			
			if(jsonobj.has("name")){
				name=jsonobj.getString("name");
			}
			
			if(jsonobj.has("start_time")){
				SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
				startTime = simpleDateFormat.parse(jsonobj.getString("start_time"));
			}
			if(jsonobj.has("tag")){
					tags=jsonobj.getString("tag");
	        }
			
		} catch (org.json.JSONException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        Integer interestedNumber=0;
        Date createTime = new Date();
        Entity event = new Entity("Event", eventKey);
        event.setProperty("createTime", createTime);
        event.setProperty("id", id);
        event.setProperty("interestedNumber", interestedNumber);
        event.setProperty("url", url);
        event.setProperty("name", name);
        event.setProperty("startTime", startTime);
        event.setProperty("tag", tags);
        
        
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(event);
        Query query = new Query("Event", eventKey).addSort("date", Query.SortDirection.DESCENDING);
        List<Entity> events = datastore.prepare(query).asList(FetchOptions.Builder.withLimit(100));
        resp.getWriter().write(ToJSON.ListToJSON(events,"events"));
    }
}
